from tools.db import *
import numpy as np
import matplotlib.pyplot as plt
from pyecharts import Bar
from pyecharts import Pie
import os

'''数据分析、机器学习、人工智能相关职位在各大城市的分布'''
def city(keywords):

    Post = lagou_post_info


    a = '%' + keywords[0] + '%'
    b = '%' + keywords[1] + '%'
    c = '%' + keywords[2] + '%'


    mydata = Post.select(Post.city, fn.COUNT(Post.id).alias('num')).where(Post.post_name % a| Post.post_name % b| Post.post_name % c).group_by(Post.city)
    labels = ['北京', '上海', '广州', '深圳', '杭州', '天津', '西安', '苏州', '武汉', '厦门', '长沙', '成都']
    number = [0]*len(labels)
    for item in mydata:
        if item.city in labels:
            dex = labels.index(item.city)
            number[dex] = item.num
    print(number)

    #柱状图
    title = u'{}职位在各大城市的分布'.format(keywords[0])
    bar = Bar(title, title_text_size=25, title_pos="center", width=800, height=500)
    bar.add("", labels, number, is_random=True, xaxis_label_textsize=20, yaxis_label_textsize=20, is_label_show=True, label_text_size=15, legend_text_size=15, legend_orient='vertical',legend_pos='right', levisual_text_color="#fff", symbol_size=15)
    bar.show_config()

    bar.render('./templates/city_bar.html')
    # bar.render('a.jpg')
    #饼图
    pie = Pie(title, title_text_size=25, title_pos="center", width=800, height=500)
    pie.add("", labels, number, is_label_show=True, label_text_size=15, legend_text_size=15, legend_orient='vertical',legend_pos='right', levisual_text_color="#fff", symbol_size=15)
    pie.show_config()
    # pie.render('../templates/city_pie.html')
    pie.render('./templates/city_pie.html')
    # bar.render('b.jpg')

    # plt.savefig("../static/img/ana2.png")

if __name__ == '__main__':
    keywords = ['数据', '人工智能', '机器学习']
    #     word_cloud(keywords)
    city(keywords)

